﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using ICSharpCode.SharpZipLib.Zip;
using System.Threading;

public partial class Controls_Template_SearchResult : System.Web.UI.UserControl
{
    class_database db;
    CFile cFile;
    CMauVirus cMau;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        DisplayResultSearch();
    }


    protected DataTable GetResultSearch(string keySearch, string typeSearch)
    {
        string sql = "";
        if (typeSearch == "0")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_mau.`name` LIKE '%" + keySearch + "%'";
        }
        else if (typeSearch == "1")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_loaimau.`name` LIKE '%" + keySearch + "%'";
        }
        else if (typeSearch == "2")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_mau.tenhamdiet LIKE '%" + keySearch + "%'";
        }
        else if (typeSearch == "3")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_user.`name` LIKE '%" + keySearch + "%'";
        }
        else if (typeSearch == "4")
        {

        }
        else if (typeSearch == "5")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_suvu.`name` LIKE '%" + keySearch + "%'";
        }
        else if (typeSearch == "6")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_loaimalware.`name` LIKE '%" + keySearch + "%'";
        }
        else if (typeSearch == "7")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_hanhvi.`name` LIKE '%" + keySearch + "%'";
        }
        else if (typeSearch == "8")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_kithuatanti.`name` LIKE '%" + keySearch + "%'";
        }
        else if (typeSearch == "9")
        {
            sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_mau.domainserverconnect LIKE '%" + keySearch + "%'";
        }
        db = new class_database();
        DataTable dt = new DataTable();
        dt = db.GetDataTable(sql);
        return dt;
    }

    protected void SetHyperLink(string idMau, RepeaterItem item)
    {
        db = new class_database();
        string sql = "SELECT idloaibai FROM tbl_baiphantich WHERE idmau = " + idMau;
        DataTable dt = db.GetDataTable(sql);

        HyperLink linkThongTinChung = item.FindControl("linkThongTinChung") as HyperLink;
        HyperLink linkThongTinKyThuatChiTiet = item.FindControl("linkThongTinKyThuatChiTiet") as HyperLink;
        HyperLink linkThongTinFile = item.FindControl("linkThongTinFile") as HyperLink;
        HyperLink linkThongTinHamDiet = item.FindControl("linkThongTinHamDiet") as HyperLink;
        HyperLink linkData = item.FindControl("linkData") as HyperLink;

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["idloaibai"].ToString() == "1")
                {
                    linkThongTinChung.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=ThongTinChung&idMau=" + idMau;
                    linkThongTinChung.Attributes.Add("style", "display: block");
                }
                else if (dt.Rows[i]["idloaibai"].ToString() == "2")
                {
                    linkThongTinKyThuatChiTiet.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=ThongTinKyThuatChiTiet&idMau=" + idMau;
                    linkThongTinKyThuatChiTiet.Attributes.Add("style", "display: block");
                }
                else if (dt.Rows[i]["idloaibai"].ToString() == "3")
                {
                    linkThongTinFile.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=ThongTinFile&idMau=" + idMau;
                    linkThongTinFile.Attributes.Add("style", "display: block");
                }
                else if (dt.Rows[i]["idloaibai"].ToString() == "4")
                {
                    linkThongTinHamDiet.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=ThongTinHamDiet&idMau=" + idMau;
                    linkThongTinHamDiet.Attributes.Add("style", "display: block");
                }
                else if (dt.Rows[i]["idloaibai"].ToString() == "5")
                {
                    linkData.Attributes.Add("style", "display: block");
                    //linkData.NavigateUrl = "../../Default.aspx?module=Template&view=General&nameBV=Data&idMau=" + idMau;
                    linkData.NavigateUrl = "javascript:Download(" + idMau + "); return false;";
                }
            }
        }
    }

    protected DataTable GetResultDateSearch(string from, string to)
    {
        DataTable dt = new DataTable();
        string sql = "SELECT tbl_mau.id, tbl_mau.date, tbl_mau.name, tbl_loaimau.name as LoaiMau, tbl_mau.tenhamdiet, tbl_user.`name` as NguoiPhanTich," +
            " tbl_mau.date, tbl_mau.suvu, tbl_loaimalware.name as LoaiMalware, tbl_hanhvi.`name` as HanhVi," +
            " tbl_kithuatanti.`name` as KiThuatAnti, tbl_mau.domainserverconnect" +
            " FROM tbl_mau, tbl_loaimau, tbl_user, tbl_loaimalware, tbl_hanhvi, tbl_kithuatanti" +
            " WHERE tbl_mau.type = tbl_loaimau.id" +
            " AND tbl_mau.nguoiphantich = tbl_user.id" +
            " AND tbl_mau.loaimalware = tbl_loaimalware.id" +
            " AND tbl_mau.hanhvi = tbl_hanhvi.id" +
            " AND tbl_mau.kithuatanti = tbl_kithuatanti.id" +
            " AND tbl_mau.date >= '" + from + "' AND tbl_mau.date <= '" + to + "'";
        db = new class_database();
        dt = db.GetDataTable(sql);
        return dt;
    }

    private void Download(string idMau)
    {
        CFile cFile = new CFile();
        string pathData = System.Web.Configuration.WebConfigurationManager.AppSettings["dataPath"].ToString();
        string pathDirectory = pathData + "/" + idMau;
        bool existDirect = cFile.CheckExitsFileOrDirect("0", pathDirectory);
        bool existFile = false;
        if (existDirect == true)
            {
                existFile = cFile.CheckExitsFileOrDirect("1", pathDirectory);
            }
            if (existDirect == true && existFile == true)
            {
                DownLoadFile(idMau);
            }
            else
            {
                class_database.Show("Không có dữ liệu để download !!!");
            }
    }

    private void DownLoadFile(string idMau)
    {
        //StartZip(idMau);
        if (cFile.CreateZip(idMau))
        {
            string nameVirus = cMau.GetNameMauFromID(idMau);
            string fileName = nameVirus + ".zip";
            string pathData = System.Web.Configuration.WebConfigurationManager.AppSettings["dataPath"].ToString();
            string fileURL = pathData + "/" + idMau + "/" + fileName;
            if (File.Exists(fileURL))
            {
                Page.Response.Clear();

                // Download thành công thì xóa đi file zip vừa nén
                bool success = CFile.ResponseFile(Page.Request, Page.Response, fileName, fileURL, 1024000);
                if (success)
                {
                    File.Delete(fileURL);
                }
                else
                {
                    class_database.Show("Download không thành công !!!");
                }
            }
            Page.Response.End();
        }
        else
        {

        }

    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {
        string idMau = "";
        idMau = hdnIDMau.Value.ToString();
        if (idMau != "")
        {
            Download(idMau);
        }        
    }

    protected void DisplayResultSearch()
    {
        string typeSearch = Session["typeSearch"].ToString();
        if (typeSearch == "4")
        {
            string from = Session["from"].ToString();
            string to = Session["to"].ToString();
            DataTable dt = GetResultDateSearch(from, to);
            rptResultSearch.DataSource = dt;
            rptResultSearch.DataBind();
        }
        else
        {
            string keySearch = Session["keySearch"].ToString();
            DataTable dt = GetResultSearch(keySearch, typeSearch);
            rptResultSearch.DataSource = dt;
            rptResultSearch.DataBind();
        }

        foreach (RepeaterItem item in rptResultSearch.Items)
        {
            Button btnEdit = item.FindControl("btnEdit") as Button;
            //Button btnDelete = item.FindControl("btnDelete") as Button;
            //string sessionUserType = Session["user_type"].ToString();
            //if (sessionUserType == "0")
            //{
            //    btnEdit.Attributes.Add("style", "display: inline-block");
            //    btnDelete.Attributes.Add("style", "display: inline-block");
            //}

            string idMau = btnEdit.CommandArgument;
            SetHyperLink(idMau, item);
        }

    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {

    }


}